package com.linln.admin.app.service.task;

import com.linln.admin.app.domain.RepoAccount;
import com.linln.admin.app.repository.InfoRepository;
import com.linln.admin.app.repository.RepoAccountRepository;
import com.linln.admin.app.repository.VersionAppRepository;
import com.linln.admin.app.repository.VersionRepository;
import com.linln.admin.system.domain.TaskQueue;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

public abstract class AbstractTaskExecutor {
    @Autowired
    protected VersionRepository versionRepository;
    @Autowired
    protected VersionAppRepository versionAppRepository;
    @Autowired
    protected InfoRepository infoRepository;
    @Autowired
    protected RepoAccountRepository accountRepository;

    /**
     * 是否可以处理这个任务
     *
     * @param t
     * @return
     */
    public abstract boolean support(TaskQueue t);

    /**
     * 处理任务，并返回处理结果
     *
     * @param t
     * @return true-成功; 其它-false
     */
    public abstract boolean exec(TaskQueue t);

    protected RepoAccount getRepoAccount(Long repoAccountId) {
        Optional<RepoAccount> ora = accountRepository.findById(repoAccountId);
        return ora.isPresent() ? ora.get() : null;
    }
}
